Linkedin Java 檢定題庫 static import


Posted by c9103205 on 2021-07-01

前言
在更新Linkedkin 個人檔案的時候
偶然發現他有技術檢定測驗
如果總成績在前30%,會發給你技術認證徽章
如果第一次沒考過可以重考
如果第二次也沒考過就要"等半年!!!" 才能考第二次
本文章為實際考題
個人感覺題目有點像OCA的考題,蠻適合正在學習Java觀念
或工作一段時間但沒深入研究Java的
如果有需要的人還請自行服用。

Quesion:
Normally , to acess a static member of a class such as Math.PI,
you would need to specify the class "Math". What would be the best way to allow you yo use simple "PI" in your code ?

A. Add a static import .
B. Declare local copies of the constants in your code.
C. Put the static members in an interface and inherit from that interface.
D. This cannot bedone . You must always qualify references to static memberss with the class frome which they came.

answer: A
解析:
這題目大家第一次看可能看不太懂,他的意思是如果我想直接使用Math中的PI常數,要怎麼做呢?

我們可以點進去Math這個class,可以找到PI前面宣告了static
 (話說小編只能背到3.1415926...國小老師不知道為什麼要我們背,跟弟子規一樣未解之謎)


    /**
     * The {@code double} value that is closer than any other to
     * <i>pi</i>, the ratio of the circumference of a circle to its
     * diameter.
     */
    public static final double PI = 3.14159265358979323846;

而一般ide在引入static參數時,會這樣建議,習慣上也會這樣用:

    public static void main(String[] args) {
        System.out.println(Math.PI); //class.variable
        //印出 3.141592653589793
}

但其實也可以這樣import,但比較少見

import static java.lang.Math.*;

class Main {
    public static void main(String[] args) {
        System.out.println(PI); //不須加上Math,因為在上面已經import
        //印出 3.141592653589793
    }
}

#java #linkedin







Related Posts

【JS 大魔王 - 1】閉包 Closure 與 Scope 作用域

【JS 大魔王 - 1】閉包 Closure 與 Scope 作用域

Git 基礎概念與版本控制操作

Git 基礎概念與版本控制操作

The introduction and difference between class component and function component in React

The introduction and difference between class component and function component in React


Comments